class_name SeekContextBehavior
extends BaseContextBehavior

#region 属性
@export var target_reached_threshold:float = 0.5
@export var slow_radius:float = 2
var _reached_last_target:bool = true
var _target_pos_cached:Vector2
#region Draw
var _draw_directions_temp:Array[Vector2] = []
var _draw_interests_temp:Array[float] = []
#endregion
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_context(steering_component:SteeringComponent,resolution_radio:int):
	super.init_context(steering_component,resolution_radio)
func get_interest(entity:CharacterEntity,directions:Array[Vector2]) -> Array[float]:
	interest = []
	interest.resize(directions.size())
	var data = entity.get_entity_data()
	var detector = entity.get_detector(GameEnum.DetectorType.Target)
	var closed_entity = detector.get_closed_entity(GameEnum.TargetDetectorTargetType.VisibleTarget if not data.is_sky_view else GameEnum.TargetDetectorTargetType.Target,global_position) if detector else null
	if _reached_last_target:
		if not closed_entity:
			return interest
		else:
			_reached_last_target = false
			_target_pos_cached = closed_entity.get_pos()
	if not data.is_sky_view:
		if (_target_pos_cached - global_position).length() < target_reached_threshold:
			_reached_last_target = true
			return interest
		var target_vec:Vector2 = _target_pos_cached - global_position
		var target_dir:Vector2 = target_vec.normalized()
		var is_slow = target_vec.length() < slow_radius
		for i in range(directions.size()):
			interest[i] = directions[i].dot(target_dir) * (target_vec.length()/slow_radius if is_slow else 1) * sensitive
	else:
		var navigation_agent_2d:NavigationAgent2D = entity.navigation_agent_2d
		if not navigation_agent_2d:
			return interest
		navigation_agent_2d.target_position = _target_pos_cached
		if navigation_agent_2d.is_navigation_finished():
			_reached_last_target = true
			return interest
		print(navigation_agent_2d.get_next_path_position())
		var target_vec:Vector2 = navigation_agent_2d.get_next_path_position() - global_position
		var target_dir:Vector2 = target_vec.normalized()
		var is_slow = target_vec.length() < slow_radius
		for i in range(directions.size()):
			interest[i] = directions[i].dot(target_dir) * (target_vec.length()/slow_radius if is_slow else 1) * sensitive
	_draw_directions_temp = directions
	_draw_interests_temp = interest
	return interest
func get_danger(entity:CharacterEntity,directions:Array[Vector2]) -> Array[float]:
	danger = []
	danger.resize(directions.size())
	return danger
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _draw() -> void:
	super._draw()
	for i in range(_draw_directions_temp.size()):
		draw_line(global_position,_draw_directions_temp[i]*_draw_interests_temp[i]*30 + global_position,Color.CHARTREUSE)
#endregion
